﻿using StarPizza.Model.BaseModel;
using StarPizza.Model.Core;
using StarPizza.ViewModel.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarPizza.ViewModel.ClientSystem
{
    class PizzaSelector : PizzaOrderer
    {
        Catalogue catalogue;

        private Dictionary<PizzaTemplate, PizzaVM> mappedPizzaVMs;
        public ObservableCollection<PizzaVM> Menu { get; set; }
        public PizzaVM SelectedPizzaVM { get; set; }
        public PizzaSelector(Catalogue catalogue, OrderController orderController, DoughLister doughLister)
            : base(orderController, doughLister)
        {
            this.catalogue = catalogue;
            mappedPizzaVMs = new Dictionary<PizzaTemplate, PizzaVM>();
            Menu = new ObservableCollection<PizzaVM>();
            SetupPizzaMenu();
            catalogue.AvailablePizzasChanged += catalogue_AvailablePizzasChanged;
            catalogue.NewPizzaAdded += catalogue_NewPizzaAdded;
            doughLister.IngredientVMSelected += doughListerSelector_DoughVMSelected;
        }
        void doughListerSelector_DoughVMSelected(DoughVM dough)
        {
            foreach (var kvp in mappedPizzaVMs)
            {
                var pizzaVM = kvp.Value;
                pizzaVM.Dough = dough.WrappedDough;
            }
        }
        PizzaVM PreparePizzaVM(PizzaTemplate pizzaTemplate)
        {
            var pizzaVM = PreparePizzaVMBase();
            mappedPizzaVMs[pizzaTemplate] = pizzaVM;
            pizzaVM.PizzaTemplate = pizzaTemplate;
            return pizzaVM;
        }
        void RegisterPizzaTemplate(PizzaTemplate pizzaTemplate)
        {
            var pizzaVM = PreparePizzaVM(pizzaTemplate);
            if (catalogue.IsPizzaTemplateOrderable(pizzaTemplate))
            {
                Menu.Add(pizzaVM);
            }
        }
        void SetupPizzaMenu()
        {
            foreach (var pizzaTemplate in catalogue.GetAllPizzas())
            {
                RegisterPizzaTemplate(pizzaTemplate);
            }
        }
        void catalogue_NewPizzaAdded(PizzaTemplate pizzaTemplate)
        {
            RegisterPizzaTemplate(pizzaTemplate);
        }
        void catalogue_AvailablePizzasChanged()
        {
            foreach (var kvp in mappedPizzaVMs)
            {
                var pizzaVM = kvp.Value;
                if (!catalogue.IsPizzaTemplateOrderable(pizzaTemplate: kvp.Key))
                {
                    if (Menu.Contains(pizzaVM))
                    {
                        Menu.Remove(pizzaVM);
                    }
                }
                else if (!Menu.Contains(pizzaVM))
                {
                    Menu.Add(pizzaVM);
                }
            }
        }
        public void OrderSelectedPizzaVM()
        {
            OrderPizza(SelectedPizzaVM.PizzaTemplate);
        }
    }
}
